
var api_basedomain='http://pin.zhiyunzhushou.com';
var socket_ip = 'ws://pin.zhiyunzhushou.com:8282';

function fnLoadImage(ele_) {
    var dataUrl = $api.attr(ele_, 'data-url');
    if (dataUrl) {
        api.imageCache({
            url: dataUrl
        }, function(ret, err) {
            if (ret) {
                ele_.src = ret.url;
                $api.attr(ele_, 'data-url', '');
            } else {}
        });
    }
}
function checklogin()
{
       var token = $api.getStorage('token');
       console.log(token);
       if(typeof(token) == "undefined"){
         console.log('checklogin');
         api.openWin({
             name: 'home_login',
             url: 'widget://html/home/login.html',
              reload: true
         });

       }

}
//退出事件监听
function closeapp() {
	var keyback_num = 0;
	api.addEventListener({
		name: 'keyback'
	}, function(ret, err) {
		if(keyback_num) {
			api.closeWidget({
				silent: true
			})
		}
		$page.toast('再按一次退出');
		keyback_num++;
	});
}

function closewin(name) {
    name = name || '';
    api.closeWin({
        name: name
    });


}


var $page = {};
$page.init = function() {
     var header = $api.byId("header");
     console.log(header);
     if(header !==null){
      // $api.fixStatusBar(header);
     }
    //监听事件
}

 $page.toast=function(msg,location){
   location= location ||'bottom';
   api.toast({
       msg: msg,
       duration: 2000,
       location: location
   });
 }
$page.openwin = function(name, url, param, reload) {
  var page=['product_checkorder','home_address','home_chat','home_coupon','home_fav','home_orderdetail','home_orderlist','home_wuliu','home_userinfo'];
    var token = $api.getStorage('token');
  if(page.indexOf(name)!==-1 && typeof(token)==='undefined'){
       $page.openwin('home_login','widget://html/home/login.html');
       return;
  }
    param = param || {};
    reload = reload || true;
    api.openWin({
        name: name,
        url: url,
        pageParam: param,
        reload: reload
    });
}
$page.openfrm = function(name, url, rect, param, bounces, reload) {
    bounces = bounces || false;
    reload = reload || false;
    param = param || {};
    api.openFrame({
        name: name,
        url: url,
        rect: rect,
        pageParam: param,
        bounces: bounces,
        reload: reload,
        vScrollBarEnabled: false,
        hScrollBarEnabled: false
    });

}

$page.pullup = function(cbk) {
    api.addEventListener({
        name: 'scrolltobottom',
        extra: {
            threshold: 20 //设置距离底部多少距离时触发，默认值为0，数字类型
        }
    }, function(ret, err) {
        console.log("pullup..");
        cbk(ret);
    });
};


$page.get_data = function(url, method, data, cbk) {

    var posturl = api_basedomain + url;
    $api.post(posturl, {
        values: data
    }, function(ret, err) {

        console.log("----postdata---");
        console.log(posturl);
        console.log(JSON.stringify(data));
        console.log("----postdata---");
        console.log("----postret---");
        console.log(JSON.stringify(ret));
        console.log("----postret---");

         cbk(ret);
    });


}
$page.switchnav = function(e, cssname) {
    var el = e.currentTarget;
    var pnodes = el.parentNode;
    var cldnodes = pnodes.children;

    for (var i = 0; i < cldnodes.length; i++) {

        if (cldnodes[i] == el) {
            $api.addCls(el, cssname);
        } else {
            $api.removeCls(cldnodes[i], cssname);
        }
    }
}
$page.pullreload = function(cbk) {
    api.setRefreshHeaderInfo({
        visible: true,
        //	loadingImg: 'widget://image/refresh.png',
        bgColor: '#f0f0f0',
        textColor: '#888',
        textDown: '下拉刷新...',
        textUp: '松开刷新...',
        showTime: false
    }, function(ret, err) {
        api.refreshHeaderLoadDone();
        cbk(ret)
    });
};

var timetool = {};
timetool.leftpad = function(str, len, ch) {
    str = String(str);
    var i = -1;
    if (!ch && ch !== 0) ch = ' ';
    len = len - str.length;
    while (++i < len) {
        str = ch + str;
    }
    return str;

};
timetool.countTime = function(endtimestamp, el) {
    //获取当前时间
    var date = new Date();
    var now = date.getTime();
    //设置截止时间
    //  var endDate = new Date(endtime);//2017-09-30 02:02:02
    // var end = endDate.getTime();
    //时间差

    var leftTime = endtimestamp * 1000 - now;
    if (leftTime < 0) {
        $api.html('00:00:00');

        return true;
    }
    //定义变量 d,h,m,s保存倒计时的时间
    var d, h, m, s;
    if (leftTime >= 0) {
        d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
        h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
        m = Math.floor(leftTime / 1000 / 60 % 60);
        s = Math.floor(leftTime / 1000 % 60);
    }
    var tiptxt = '';
    if (d > 0) {
        tiptxt = this.leftpad(d, 2, '0') + "天:";
    }
    // if(h>0){
    tiptxt += this.leftpad(h, 2, '0') + ":";
    //}
    if (m > 0) {
        tiptxt += this.leftpad(m, 2, '0') + ":";
    }
    tiptxt += this.leftpad(s, 2, '0');


    //将倒计时赋值到div中

    $api.html(el, tiptxt);

    //递归每秒调用countTime方法，显示动态时间效果
    if (leftTime) {
        setTimeout("timetool.countTime('" + endtimestamp + "','" + el + "')", 1000);
    }
}

function unique_arr(a) {

    var res = a.filter(function(item, index, array) {
        return array.indexOf(item) === index;
    });

    return res;
}

function removeByValue(arr, val) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == val) {
            arr.splice(i, 1);
            break;
        }
    }

}
